package com.block.common.vo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.block.common.enums.AppRspCodeEnum;

import java.util.Collection;
import java.util.Map;

public  class ResponseVo<T>{
	private String rspCode;
	private String rspMsg;
	@JSONField(serialize = false)
	private T tdata;

	@JSONField(serialize = false)
	public T getObjdData(){
		return this.tdata;
	}
	public String getFlowNo() {
		return flowNo;
	}

	public void setFlowNo(String flowNo) {
		this.flowNo = flowNo;
	}

	//流水号
	private String flowNo;

	private JSONObject data;

	public JSONArray getArrayData() {
		return arrayData;
	}

	public void setArrayData(JSONArray arrayData) {
		this.arrayData = arrayData;
	}

	private JSONArray arrayData;

	public String getRspCode() {
		return rspCode;
	}
	public void setRspCode(String rspCode) {
		this.rspCode = rspCode;
	}
	public JSONObject getData() {
		return data;
	}
	public void setData(JSONObject data) {
		this.data = data;
	}


	
	public String getRspMsg() {
		return rspMsg;
	}
	public void setRspMsg(String rspMsg) {
		this.rspMsg = rspMsg;
	}

	public T getTdata() {
		return tdata;
	}

	public void setTdata(T tdata) {
		this.tdata = tdata;
	}

	public ResponseVo(){}
	
	/**
	 * 设置返回码和返回数据
	 * @param rspCode
	 * @param data
	 */
	public ResponseVo(String rspCode, JSONObject data, String flowNo) {
		
		this.rspCode = rspCode;
		this.rspMsg= AppRspCodeEnum.getRspMsg(rspCode);
		this.data = data;
		this.flowNo=flowNo;
	}

	/**
	 * 将集合作为返回的data
	 * @param respCode
	 * @param list
	 */
	public ResponseVo(String respCode, Collection<Object> list)
	{
		this.rspCode = respCode;
		this.arrayData= JSONArray.parseArray(JSONArray.toJSONString(list));
	}


	/**
	 * 将map作为返回的data
	 * @param rspCode
	 * @param map
	 */
	public ResponseVo(String rspCode, Map<String,Object> map)
	{
		this.rspCode = rspCode;
		this.rspMsg=AppRspCodeEnum.getRspMsg(rspCode);
		this.data= JSONObject.parseObject(JSONObject.toJSONString(map));
	}

	public ResponseVo(String rspCode, T t)
	{
		this.rspCode = rspCode;
		this.rspMsg=AppRspCodeEnum.getRspMsg(rspCode);

		this.data= JSONObject.parseObject(JSONObject.toJSONString(t));
		this.tdata=t;

	}

	public ResponseVo(String rspCode,String rspMsg)
	{
		this.rspMsg = rspMsg;
		this.rspCode = rspCode;
	}


	public ResponseVo(String rspCode, JSONObject jsonObject)
	{
		this.rspCode = rspCode;
		this.rspMsg=AppRspCodeEnum.getRspMsg(rspCode);
		this.data=jsonObject;
	}

	public ResponseVo(String rspCode, JSONArray arrayData) {
		this.rspCode = rspCode;
		this.arrayData = arrayData;
	}

	/**
	 * 自定义返回信息
	 * @param rspCode
	 * @param rspMsg
	 */
	public ResponseVo(String rspCode, String rspMsg, String flowNo)
	{
		this.rspCode = rspCode;
		this.rspMsg=rspMsg;
		this.flowNo=flowNo;

	}
	/**
	 * 自定义返回信息
	 * @param rspCode
	 * @param rspMsg
	 */
	public ResponseVo(String rspCode, String rspMsg, String flowNo, String ... replace)
	{
		this.rspCode = rspCode;
		this.rspMsg=String.format(rspMsg,replace);
		this.flowNo=flowNo;

	}

	/**
	 * 设置返回码
	 * @param rspCode
	 */
	public ResponseVo(String rspCode) {
		
		this.rspCode = rspCode;
		this.rspMsg=AppRspCodeEnum.getRspMsg(rspCode);
	}

	public ResponseVo(String rspCode, T t,boolean flag)
	{
		this.rspCode = rspCode;
		this.rspMsg=AppRspCodeEnum.getRspMsg(rspCode);
		if (flag)
			this.data= JSONObject.parseObject(JSONObject.toJSONString(t));
		this.tdata=t;
	}

	@Override
	public String toString() {
		return "ResponseVo{" +
				"rspCode='" + rspCode + '\'' +
				", rspMsg='" + rspMsg + '\'' +
				", tdata=" + tdata +
				", flowNo='" + flowNo + '\'' +
				", data=" + data +
				", arrayData=" + arrayData +
				'}';
	}
}
